//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Windows;
using OpenLS.Spreadsheet.Expressions;
using OpenLS.Spreadsheet.UI.Internals;
using OpenLS.Spreadsheet.UI.Internals.Resources;
using OpenLS.UI;

namespace OpenLS.Spreadsheet.UI
{
    /// <summary>
    /// Interaction logic for NameManagerWindow.xaml
    /// </summary>

    public partial class NameManagerControl 
    {

        public NameManagerControl()
        {
            this.Resources.MergedDictionaries.Add(new DialogResources());
            InitializeComponent();
        }
        void OnNew(object sender, object args)
        {
            NewDefinedNameControl w = new NewDefinedNameControl();
            Visibility = Visibility.Hidden;
            Worksheet wb = (Worksheet)DataContext;
            DefinedName newName = new DefinedName(wb.Workbook);
            newName.Expression = new RangeReferenceExpression( wb.Selection.ToReference(),wb);
            w.DataContext = newName;
            WindowServices.ShowDialog(w, UIStringTable.NewName, 
                                      sender as UIElement,
                                      delegate
                                          {
                                              wb.Workbook.DefinedNames.Add(newName);
                                              Visibility = Visibility.Visible;
                                          });
            Visibility = Visibility.Visible;
        }

        private void OnEdit(object sender, RoutedEventArgs e)
        {
            DefinedName n = (DefinedName) lv.SelectedItem ;
            NewDefinedNameControl w = new NewDefinedNameControl();
            w.DataContext = n;
            WindowServices.ShowDialog(w, UIStringTable.NewName,
                          sender as UIElement,
                          delegate
                          {
                          });


        }

        private void OnDelete(object sender, RoutedEventArgs e)
        {
            Worksheet ws = (Worksheet) DataContext;
            DefinedName n = (DefinedName)lv.SelectedItem;
            ws.Workbook.DefinedNames.Remove(n);
        }
    }
}